package com.example.admin.mybledemo.ota;

/**
 * OTA配置类
 * 从OtaManager中提取，提供配置管理功能
 */
public class OtaConfig {
    
    // 默认配置常量
    private static final int DEFAULT_MAX_RETRY_COUNT = 3;
    private static final int DEFAULT_CONNECT_TIMEOUT = 15000;
    private static final int DEFAULT_WRITE_TIMEOUT = 10000;
    private static final int DEFAULT_BLOCK_DELAY = 50;
    private static final int DEFAULT_VERSION_CHECK_RETRY = 2;
    private static final int DEFAULT_CREDENTIAL_CHECK_RETRY = 2;
    private static final int DEFAULT_DATA_TRANSFER_RETRY = 5;
    
    // 功能开关
    public boolean enableVersionCheck = true;
    public boolean enableRomCheck = true;
    public boolean enableCredentialCheck = true;
    public boolean enableRandomMac = true;
    public boolean enableLifecycleManagement = true;
    
    // 固件版本信息
    private String firmwareRomVersion = "";
    private String firmwareVersion = "";
    
    // 协议配置
    public ProtocolVersion protocolVersion = ProtocolVersion.AUTO;
    
    // 重试配置
    public int maxRetryCount = DEFAULT_MAX_RETRY_COUNT;
    public int versionCheckRetry = DEFAULT_VERSION_CHECK_RETRY;
    public int credentialCheckRetry = DEFAULT_CREDENTIAL_CHECK_RETRY;
    public int dataTransferRetry = DEFAULT_DATA_TRANSFER_RETRY;
    
    // 超时配置
    public int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
    public int writeTimeout = DEFAULT_WRITE_TIMEOUT;
    public int blockDelay = DEFAULT_BLOCK_DELAY;
    
    /**
     * 创建默认配置
     */
    public static OtaConfig createDefault() {
        return new OtaConfig();
    }
    
    /**
     * 创建快速配置（减少重试次数，适合演示）
     */
    public static OtaConfig createFast() {
        OtaConfig config = new OtaConfig();
        config.maxRetryCount = 1;
        config.versionCheckRetry = 1;
        config.credentialCheckRetry = 1;
        config.dataTransferRetry = 2;
        config.connectTimeout = 8000;
        config.blockDelay = 20;
        return config;
    }
    
    /**
     * 创建稳定配置（增加重试次数，适合生产环境）
     */
    public static OtaConfig createStable() {
        OtaConfig config = new OtaConfig();
        config.maxRetryCount = 5;
        config.versionCheckRetry = 3;
        config.credentialCheckRetry = 3;
        config.dataTransferRetry = 8;
        config.connectTimeout = 20000;
        config.blockDelay = 100;
        return config;
    }
    
    /**
     * 验证配置有效性
     */
    public boolean validate() {
        return maxRetryCount > 0 && 
               connectTimeout > 0 && 
               writeTimeout > 0 &&
               versionCheckRetry > 0 &&
               credentialCheckRetry > 0 &&
               dataTransferRetry > 0;
    }
    
    /**
     * 转换为字符串描述
     */
    @Override
    public String toString() {
        return String.format("OtaConfig{protocol=%s, maxRetry=%d, timeout=%d, blockDelay=%d}", 
                protocolVersion.getDescription(), 
                maxRetryCount, 
                connectTimeout,
                blockDelay);
    }
    
    /**
     * 设置固件版本信息
     */
    public void setFirmwareVersionInfo(String romVersion, String softwareVersion) {
        this.firmwareRomVersion = romVersion;
        this.firmwareVersion = softwareVersion;
    }
    
    /**
     * 获取固件ROM版本
     */
    public String getFirmwareRomVersion() {
        return firmwareRomVersion;
    }
    
    /**
     * 获取固件软件版本
     */
    public String getFirmwareVersion() {
        return firmwareVersion;
    }
    
    /**
     * 检查是否启用版本校验
     */
    public boolean isVersionCheckEnabled() {
        return enableVersionCheck;
    }
    
    /**
     * 复制配置
     */
    public OtaConfig copy() {
        OtaConfig copy = new OtaConfig();
        copy.enableVersionCheck = this.enableVersionCheck;
        copy.enableRomCheck = this.enableRomCheck;
        copy.enableCredentialCheck = this.enableCredentialCheck;
        copy.enableRandomMac = this.enableRandomMac;
        copy.enableLifecycleManagement = this.enableLifecycleManagement;
        copy.protocolVersion = this.protocolVersion;
        copy.maxRetryCount = this.maxRetryCount;
        copy.versionCheckRetry = this.versionCheckRetry;
        copy.credentialCheckRetry = this.credentialCheckRetry;
        copy.dataTransferRetry = this.dataTransferRetry;
        copy.connectTimeout = this.connectTimeout;
        copy.writeTimeout = this.writeTimeout;
        copy.blockDelay = this.blockDelay;
        copy.firmwareRomVersion = this.firmwareRomVersion;
        copy.firmwareVersion = this.firmwareVersion;
        return copy;
    }
} 